home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / gengetopt-2.6.lha / gengetopt-2.6 / README < prev    next >
Text File  |  2002-03-02  |  21KB  |  572 lines

  1. GNU Gengetopt 2.6
  2.  
  3. March 2nd, 2002
  4.  
  5. This program generates a C function that uses getopt_long function to parse
  6. the command line options, to validate them and fills a struct.
  7.  
  8. Thus your program can now handle options such as:
  9.  
  10. myprog --input foo.c -o foo.o --no-tabs -i 100 *.class
  11.  
  12. And both long options (those that start with --) and short options (start
  13. with - and consist of only one character) can be handled. For standards
  14. about short and long options you may want to take a look at the GNU Coding
  15. Standards.
  16.  
  17. gengetopt is free software. Please see the file LICENSE and COPYING for
  18. details.
  19. Notice that: Use of gengetopt does not impose any particular license on the
  20. generated code: the code generated is not under any license.
  21.  
  22. For documentation, please read this file.
  23.  
  24. gengetopt is perfect if you are too lazy (like me) to write all stuff
  25. required to call getopt_long, and when you have a
  26. program and wish it took options.
  27.  
  28. Generated code works if you use GNU Autoconf or GNU Automake.
  29.  
  30. Gengetopt has originally been written by Roberto Arturo Tena Sanchez
  31. <arturo@directmail.org>, and currently maintained by Lorenzo Bettini
  32. <bettini@gnu.org>.
  33.  
  34. Gengetopt is a GNU program and its main home page is at GNU site:
  35. http://www.gnu.org/software/gengetopt/gengetopt.html,
  36.  
  37. Download
  38.  
  39. You can download it from GNU's ftp site:
  40. ftp://ftp.gnu.org/gnu/gengetopt/ or from one of its mirrors (see
  41. http://www.gnu.org/prep/ftp.html).
  42.  
  43. I do not distribute Windows binaries anymore; since, they can be easily
  44. built by using Cygnus C/C++ compiler, available at http://www.cygnus.com/.
  45. However, if you don't feel like downloading such compiler, you can request
  46. such binaries directly to me, by e-mail (bettini@gnu.org) and I can send
  47. them to you.
  48.  
  49. You may also want to check the md5sum of the archives, which are also
  50. digitally signed by me (Lorenzo Bettini) with GNU gpg
  51. (http://www.gnupg.org). My GPG public key can be found at my home page (see
  52. at the end of this doc).
  53.  
  54. You can also get the patches, if they are available for a particular release
  55. (see below for patching from a previous version).
  56.  
  57. Changes in this release
  58.  
  59.    * fixed documentation (thanks to Francesco Potorti <pot@gnu.org>)
  60.    * function names and names used in #ifndef directives in generated
  61.      sources are canonized, thus avoiding errors by the compiler (notified
  62.      by Francesco Potorti and Alexey Mahotkin <alexm@hsys.msk.ru>)
  63.    * fixed line number when reporting an error in a .ggo file, and '\n' is
  64.      no longer needed at the end of a .ggo file (notified by Francesco
  65.      Potorti)
  66.    * in the generated parser use HAVE_STRDUP to check whether the standard
  67.      strdup function can be used, and use EXIT_SUCCESS and EXIT_FAILURE for
  68.      exit (suggested by Frans E. van Dorsselaer <frans@biabv.com>)
  69.    * do not exit with error if an empty desc for an option is given (thanks
  70.      to Excoffier Denis <Denis.Excoffier@eurocopter.com>)
  71.    * fixed a bug in creation of string for the comment header in the
  72.      generated sources (thanks to Xavier Trochu <xtrochu@yahoo.com>)
  73.    * Added default values in options for integer and string options (with
  74.      help of Ralf Zeise <Zeise@serving-gmbh.de>)
  75.  
  76. Changes in release 2.5
  77.  
  78.    * much more recent versions of getopt.h, getopt.c, getopt1.c are provided
  79.      (the previous ones were very very old!), that do not rely on alloca
  80.      anymore.
  81.    * when a flag option is given the corresponding  _given variable is set
  82.      (bug fixed by Guillaume Chazarain  <booh@altern.org>).
  83.    * Fixed documentation of sample1, as suggested by Lutz Maibaum
  84.      <lutz@gold.cchem.berkeley.edu>.
  85.    * Avoid a warning due to generated gengetopt_strdup (thanks to Brian
  86.      Minard <bminard@flatfoot.ca>)
  87.    * the generated print_help function use a different printf for every
  88.      option, thus avoiding a single printf with a possible very huge string
  89.      (thanks to Herbert Thoma <tma@iis.fhg.de>)
  90.  
  91. see also NEWS file and ChangeLog for the complete list of changes between
  92. versions (also summarized in old_changes.html).
  93.  
  94. Installation
  95.  
  96. See the file INSTALL for detailed building and installation instructions;
  97. anyway if you're used to compiling Linux software that comes with sources
  98. you may simply follow the usual procedure:
  99.  
  100. cd <source code main directory>
  101. ./configure
  102. make
  103. make install
  104.  
  105. Note: unless you specify a different install directory by --prefix option of
  106. configure (e.g. ./configure --prefix=<your home>), you must be root to 'make
  107. install'.
  108.  
  109. You can also run some tests by issuing 'make check'.
  110.  
  111. Files will be installed in the following directories:
  112.  
  113.    * Executables          -> /prefix/bin
  114.    * Docs                 -> /prefix/share/doc/gengetopt
  115.    * Man pages            -> /prefix/man
  116.    * Examples             -> /prefix/share/doc/gengetopt/examples
  117.    * Util files           -> /prefix/share/gengetopt
  118.  
  119. Default value for prefix is /usr/local but you may change it with --prefix
  120. option to configure (see above).
  121.  
  122. What you need to build gengetopt
  123.  
  124. Actually you need nothing more than a Unix C/C++ compiler.
  125.  
  126. getopt_long function is usually in the standard C library, but there may be
  127. some C libraries which don't include it; in this case you have to link the
  128. program that uses the file generated by gengetopt with the files getopt.c
  129. and getopt1.c and include getopt.h in your project. You may also need to
  130. link alloca.c.We obviously provide these files in the utility files
  131. directory (/prefix/share/gengetopt). These files are part of the GNU C
  132. library. You may want to take a look at getopt man page. Read also
  133. no_getopt_long.txt. for instuctions on how to check if getopt_long and
  134. alloca are part of the library and how to deal with their lacking (using
  135. autoconf and automake).
  136.  
  137. gengetopt has been developed under Linux, using gcc, and bison (yacc) and
  138. flex (lex), and ported under Windows with Cygnus C/C++ compiler, available
  139. at http://www.cygnus.com/. I used the excellent GNU Autoconf and Automake. I
  140. also used Autotools (ftp://ftp.ugcs.caltech.edu/pub/elef/autotools) which
  141. creates a starting source tree (according to GNU standards) with autoconf,
  142. automake starting files, and getopt_long (for command line parsing).
  143.  
  144. Actually, unless you want to develop gengetopt, you don't need all these
  145. tools to build gengetopt because I provide generated sources; you don't need
  146. neither bison (yacc) nor flex (lex), for the same reason. Actually programs
  147. that use lex generated files need to link with library libfl (or libl for
  148. lex); anyway configuration phase can discover if this library is missing and
  149. in that case it sets the program to link with a source file I provide. This
  150. hack works for flex: I don't know about lex generated scanners. But, again,
  151. this is a problem only if you develop gengetopt and you use lex.
  152.  
  153. Patching from a previous version
  154.  
  155. If you downloaded a patch, say gengetopt-1.3-1.3.1-patch.gz (i.e. the patch
  156. to go from version 1.3 to version 1.3.1), cd to the directory with sources
  157. from the previous version (gengetopt-1.3) and type:
  158.  
  159. gunzip -cd ../gengetopt-1.3-1.3.1.patch.gz | patch -p1
  160.  
  161. and restart the compilation process (if you had already run configure a
  162. simple make will do).
  163.  
  164. Usage (a little tutorial)
  165.  
  166. The command line options, which have to be handled by gengetopt generated
  167. function, are specified in a file (typically with .ggo extension). This file
  168. consist in lines of sentences with the following formats:
  169.  
  170. package <packname>
  171. version <version>
  172.  
  173. option <long> <short> <desc> <argtype> {default="<default value>"} <required>
  174. option <long> <short> <desc> flag      <onoff>
  175. option <long> <short> <desc> no
  176.  
  177. Where:
  178.  
  179. packname
  180.  
  181.      Double quoted string.
  182.  
  183. version
  184.  
  185.      Double  quoted  string.
  186.  
  187. purpose
  188.  
  189.      What the program does (even on more than one line), it will be
  190.      printed with the help. Double  quoted  string.
  191.  
  192. long
  193.  
  194.      The long option, a double quoted string with  upper and  lower
  195.      case  chars,  digits,  '-' and '.'.  No spaces allowed.  The name
  196.      of the  variables  generated  to store arguments are long options
  197.      converted to be legal C variable names.  This means, '.'  and '-'
  198.      are  both replaced by '_'. '_arg' is appended, or '_flag' for a
  199.      flag.
  200.  
  201. short
  202.  
  203.      The short option, a  single  upper  or  lower  case char, or a
  204.      digit. If a '-' is specified, then no short option is considered
  205.      for the long option (thus long options with no associated short
  206.      options are allowed).
  207.  
  208. desc
  209.  
  210.      Double  quoted  string  with  upper  and lower case chars, digits,
  211.      '-', '.' and spaces. First character must not be a space.
  212.  
  213. argtype
  214.  
  215.      string, int, short, long, float, double, longdouble or longlong.
  216.  
  217. default
  218.  
  219.      an optional default value for the option.  The value must always
  220.      be specified as a double quoted string.
  221.  
  222. required
  223.  
  224.      yes or no.
  225.  
  226. onoff
  227.  
  228.      on or off. This is the state of the flag when the program starts.
  229.      If user specifies the option, the flag toggles.
  230.  
  231. The third type of option is used when the option does not take any argument.
  232. It must not be required.
  233.  
  234. Comments begins with '#' in any place of the line and ends in the end of
  235. line.
  236.  
  237. Here's an example of such a file (the file is called sample1.ggo)
  238.  
  239.  
  240.   # file sample1.ggo
  241.   option  "str-opt"     s "A string option"      string     no
  242.   option  "my-opt"      m "Another integer option"      int     no
  243.   option  "int-opt"     i "A int option"         int        yes
  244.   option  "flag-opt"    - "A flag option"        flag       off
  245.   option  "funct-opt"   F "A function option"    no
  246.   option  "long-opt"    - "A long option"        long       no
  247.   option  "def-opt"     - "A string option with default" string
  248.   default="Hello" no
  249.  
  250. The simplest way to use gengetopt is to pass this file as the standard
  251. input, i.e.:
  252.  
  253. gengetopt < sample1.ggo
  254.  
  255. by default gengetopt generates cmdline.h and cmdline.c. Otherwise we can
  256. specify these names with a command line option:
  257.  
  258. gengetopt < sample1.ggo --file-name=cmdline1 --unamed-opts
  259.  
  260. The option --unamed-opts allows the generated command line parser to accept
  261. also names, without an option (for instance you can pass a file name without
  262. an option in front of it, and also use wildcards, such as *.c, foo*.? and so
  263. on).
  264.  
  265. In cmdline1.h you'll find the generated C struct:
  266.  
  267.  /* cmdline1.h */
  268.  
  269.  /* File autogenerated by gengetopt version 2.6  */
  270.  
  271.  #ifndef _cmdline1_h
  272.  #define _cmdline1_h
  273.  
  274.  #ifdef __cplusplus
  275.  extern "C" {
  276.  #endif /* __cplusplus */
  277.  
  278.  /* Don't define PACKAGE and VERSION if we use automake.  */
  279.  #if defined PACKAGE
  280.  #  undef PACKAGE
  281.  #endif
  282.  #define PACKAGE "sample1"
  283.  #if defined VERSION
  284.  #  undef VERSION
  285.  #endif
  286.  #define VERSION "2.0"
  287.  
  288.  struct gengetopt_args_info {
  289.    char * str_opt_arg;   /* A string option.  */
  290.    int my_opt_arg;       /* Another integer option.  */
  291.    int int_opt_arg;      /* A int option.  */
  292.    int flag_opt_flag;    /* A flag option (default=off).  */
  293.    long long_opt_arg;    /* A long option.  */
  294.    char * def_opt_arg;   /* A string option with default (default='Hello').  */
  295.  
  296.    int help_given ;      /* Whether help was given.  */
  297.    int version_given ;   /* Whether version was given.  */
  298.    int str_opt_given ;   /* Whether str-opt was given.  */
  299.    int my_opt_given ;    /* Whether my-opt was given.  */
  300.    int int_opt_given ;   /* Whether int-opt was given.  */
  301.    int flag_opt_given ;  /* Whether flag-opt was given.  */
  302.    int funct_opt_given ; /* Whether funct-opt was given.  */
  303.    int long_opt_given ;  /* Whether long-opt was given.  */
  304.    int def_opt_given ;   /* Whether def-opt was given.  */
  305.  
  306.    char **inputs ; /* unamed options */
  307.    unsigned inputs_num ; /* unamed options number */
  308.  } ;
  309.  
  310.  int cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info);
  311.  
  312.  void cmdline_parser_print_help(void);
  313.  void cmdline_parser_print_version(void);
  314.  
  315.  #ifdef __cplusplus
  316.  }
  317.  #endif /* __cplusplus */
  318.  #endif /* _cmdline1_h */
  319.  
  320. Notice that by default the generated function is called cmdline_parser (see
  321. the command line options below, to override this name), and it takes the
  322. arguments that main receives and a pointer to such a struct, that it will be
  323. filled.
  324. And here's how this function can be used inside the main program:
  325.  
  326.  /* main1.cc */
  327.  /* we try to use gengetopt generated file in a C++ program */
  328.  /* we don't use autoconf and automake vars */
  329.  
  330.  #include <iostream.h>
  331.  #include "stdlib.h"
  332.  
  333.  #include "cmdline1.h"
  334.  
  335.  int
  336.  main (int argc, char **argv)
  337.  {
  338.    gengetopt_args_info args_info;
  339.  
  340.    cout << "This one is from a C++ program" << endl ;
  341.    cout << "Try to launch me with some options" << endl ;
  342.    cout << "(type sample1 --help for the complete list)" << endl ;
  343.    cout << "For example: ./sample1 *.* --funct-opt" << endl ;
  344.  
  345.    /* let's call our cmdline parser */
  346.    if (cmdline_parser (argc, argv, &args_info) != 0)
  347.      exit(1) ;
  348.  
  349.    cout << "Here are the options you passed..." << endl;
  350.  
  351.    for ( unsigned i = 0 ; i < args_info.inputs_num ; ++i )
  352.      cout << "file: " << args_info.inputs[i] << endl ;
  353.  
  354.    if ( args_info.funct_opt_given )
  355.      cout << "You chose --funct-opt or -F." << endl ;
  356.  
  357.    if ( args_info.str_opt_given )
  358.      cout << "You inserted " << args_info.str_opt_arg << " for " <<
  359.        "--str-opt option." << endl ;
  360.  
  361.    if ( args_info.int_opt_given )
  362.      cout << "This is the integer you input: " <<
  363.        args_info.int_opt_arg << "." << endl;
  364.  
  365.    if (args_info.flag_opt_given)
  366.      cout << "The flag option was given!" << endl;
  367.  
  368.    cout << "The flag is " << ( args_info.flag_opt_flag ? "on" : "off" ) <<
  369.      "." << endl ;
  370.  
  371.    cout << args_info.def_opt_arg << "! ";
  372.  
  373.    cout << "Have a nice day! :-)" << endl ;
  374.  
  375.    return 0;
  376.  }
  377.  
  378. Now you can compile main1.cc and the cmdline1.c generated by gengetopt and
  379. link all together to obtain sample1 executable:
  380.  
  381. gcc -c cmdline1.c
  382. g++ -c main1.cc
  383. g++ -o sample1 cmdline1.o main1.o
  384.  
  385. (Here we assume that getopt_long is included in the standard C library; see
  386. 'What you need to build gengetopt' section).
  387.  
  388. Now let's try some tests with this program:
  389.  
  390. $ ./sample1 -s "hello" --int-opt 1234
  391. This one is from a C++ program
  392. Try to launch me with some options
  393. (type sample1 --help for the complete list)
  394. For example: ./sample1 *.* --funct-opt
  395. Here are the options you passed...
  396. You inserted hello for --str-opt option.
  397. This is the integer you input: 1234.
  398. The flag is off.
  399. Have a nice day! :-)
  400.  
  401. You can also pass many file names to the command line (this also shows how
  402. flags work):
  403.  
  404. $ ./sample1 *.h -i -100 -x
  405. This one is from a C++ program
  406. Try to launch me with some options
  407. (type sample1 --help for the complete list)
  408. For example: ./sample1 *.* --funct-opt
  409. Here are the options you passed...
  410. file: cmdline1.h
  411. file: cmdline2.h
  412. file: cmdline.h
  413. file: getopt.h
  414. This is the integer you input: -100.
  415. The flag is on.
  416. Have a nice day! :-)
  417.  
  418. And if we try to omit the --int-opt (or -i), which is required, we get an
  419. error:
  420.  
  421. $ ./sample1
  422. This one is from a C++ program
  423. Try to launch me with some options
  424. (type sample1 --help for the complete list)
  425. For example: ./sample1 *.* --funct-opt
  426. sample1: `--int-opt' (`-i') option required!
  427.  
  428. If you're curious you may want to take a look at the generated C file.
  429.  
  430. You may find other examples in /prefix/share/doc/gengetopt.
  431.  
  432. Warning for Windows users
  433.  
  434. If you run Windows, please remember that DOS shell does not translate
  435. wildcards, and thus the previous test which uses '*.h' will not work.
  436.  
  437. Options
  438.  
  439. This is the output of gengetopt --help:
  440.  
  441. $ gengetopt --help
  442. gengetopt 2.6
  443.  
  444. Purpose:
  445.   This program generates a C function that uses getopt_long function
  446.   to parse the command line options, validate them and fill a struct.
  447.  
  448. Usage: gengetopt [OPTIONS]...
  449.    -h         --help              Print help and exit
  450.    -V         --version           Print version and exit
  451.    -iSTRING   --input=STRING      input file (default std input)
  452.    -fSTRING   --func-name=STRING  name of generated function (default='cmdline_parser')
  453.    -FSTRING   --file-name=STRING  name of generated file (default='cmdline')
  454.    -l         --long-help         long usage line in help
  455.    -u         --unamed-opts       accept filenames
  456.               --no-handle-help    do not handle --help|-h automatically
  457.               --no-handle-version do not handle --version|-V automatically
  458.               --no-handle-error   do not exit on errors
  459.  
  460. Maintained by Lorenzo Bettini <bettini@gnu.org>
  461. Report bugs to <bug-gengetopt@gnu.org>
  462.  
  463. The options should be clear; in particular:
  464.  
  465.    * if no --func-name is given, cmdline_parser is taken by default;
  466.    * with --long-help option, the "Usage" line reports all the options; this
  467.      may be unpleasant if options are many;
  468.    * with --unamed-opts we can accept also options without a name, which, in
  469.      most case, means that we can pass many file names to the program (see
  470.      the example above when we call sample1 *.h).
  471.    * if --no-handle-help (--no-handle-version) is given the command line
  472.      --help|-h (--version|-V) is not handled automatically, so the
  473.      programmer will be able to print some other information; then the
  474.      function for printing the standard help (version) response can be used;
  475.      this function is called <parser-name>_print_help
  476.      (<parser-name>_print_version), where <parser-name> is the name
  477.      specified with ---func-name or the default cmdline_parser.
  478.    * if --no-handle-error is given, an error in the parsing does not provoke
  479.      the exit of the program; instead, since the parser function, in case of
  480.      an error, returns non 0, the program can print a help message, as
  481.      gengetopt itself does in case of an error (try it!).
  482.  
  483. You may have already guessed it: gengetopt uses gengetopt itself for command
  484. line options, and this is its specification file:
  485.  
  486.  
  487.   purpose "This program generates a C function that uses getopt_long function
  488.   to parse the command line options, validate them and fill a struct."
  489.  
  490.   option  "input"         i "input file. default std input"  string     no
  491.   option  "func-name"     f "name of generated function"  string default="cmdline_parser" no
  492.   option  "file-name"     F "name of generated file"  string default="cmdline" no
  493.   option  "long-help"     l "long usage line in help" no
  494.   option  "unamed-opts"   u "accept filenames" no
  495.   option  "no-handle-help"   - "do not handle --help|-h automatically" no
  496.   option  "no-handle-version"   - "do not handle --version|-V automatically" no
  497.   option  "no-handle-error" - "do not exit on errors" no
  498.  
  499. In particular the command line for gengetopt itself is generated with the
  500. following command:
  501.  
  502. gengetopt --input=cmdline.ggo --no-handle-version --no-handle-help --no-handle-error
  503.  
  504. Indeed when --help|-h is passed on the command line, gengetopt will call
  505. cmdline_parser_print_help() and then the lines for reporting bugs. When
  506. --version|-V is passed, it will call cmdline_parser_print_version() and then
  507. prints a copyright.
  508. If an error occurs it prints a message on the screen:
  509.  
  510. $ ./gengetopt --zzzz
  511. ./gengetopt: unrecognized option `--zzzz'
  512. Run gengetopt --help to see the list of options.
  513.  
  514. Credits
  515.  
  516. See THANKS file :-)
  517.  
  518. Feedback
  519.  
  520. Tell us if you like this software :-)
  521.  
  522. Actually we want to extend it, so if you have some ideas... The most import
  523. one will be to make gengetopt more customizable :-)
  524.  
  525. Please send all bug reports by electronic mail to:
  526. bug-gengetopt@gnu.org
  527.  
  528. Mailing Lists
  529.  
  530. The following mailing lists are available:
  531.  
  532.    * help-gengetopt@gnu.org, for generic discussions about the program and
  533.      for asking for help about it (open mailing list),
  534.      http://mail.gnu.org/mailman/listinfo/help-gengetopt
  535.    * info-gengetopt@gnu.org, for receiving information about new releases
  536.      and features (read-only mailing list),
  537.      http://mail.gnu.org/mailman/listinfo/info-gengetopt
  538.  
  539. if you want to subscribe to a mailing list just go to the URL and follow the
  540. instructions.
  541.  
  542.  Lorenzo Bettini                      Roberto Arturo Tena Sanchez
  543.  http://w3.newnet.it/bettini  or      http://arturo.directmail.org
  544.  http://infostud.dsi.unifi.it/~bettini<arturo@directmail.org>,
  545.  (very fast if you're in University)
  546.  <bettini@gnu.org>.
  547.  
  548. gengetopt is free software. See the file LICENSE and COPYING for copying
  549. conditions. Anyway we won't get offended if you send us a postcard :-)
  550.  
  551. C/C++ files are formatted with GNU Source-highlight
  552. (http://www.gnu.org/software/src-highlite/) by Lorenzo Bettini.
  553.  
  554.   ------------------------------------------------------------------------
  555.  
  556. Return to GNU's home page.
  557.  
  558. Please send FSF & GNU inquiries & questions to gnu@gnu.org. There are also
  559. other ways to contact the FSF.
  560.  
  561. Please send comments on these web pages to webmasters@gnu.org, send other
  562. questions to gnu@gnu.org.
  563.  
  564. Copyright (C) 2001 Free Software Foundation, Inc., 59 Temple Place - Suite
  565. 330, Boston, MA 02111, USA
  566.  
  567. Verbatim copying and distribution of this entire article is permitted in any
  568. medium, provided this notice is preserved.
  569.  
  570. Updated:9 Jan 2001 mhw
  571.   ------------------------------------------------------------------------
  572.